package com.woniu.order.service.impl;

import com.woniu.api.movie.MovieApi;
import com.woniu.common.entity.vo.MovieVo;
import com.woniu.common.result.Result;
import com.woniu.common.entity.Order;
import com.woniu.common.entity.vo.OrderVo;
import com.woniu.order.mapper.OrderMapper;
import com.woniu.order.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mane
 * @since 2025-03-29
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private MovieApi movieApi;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<OrderVo> getOrdersByUserId(Integer userId) {
        // 查询用户的所有订单
        List<Order> orders = orderMapper.getOrdersByUserId(userId);

        // 转换为OrderVo列表
        List<OrderVo> orderVos = orders.stream().map(order -> {
            // 根据movieID查询电影信息
            Result<MovieVo> result = movieApi.getMovieById(order.getMovieId());
            MovieVo movieVo = result.getData();

            // 封装结果
            OrderVo orderVo = new OrderVo();
            orderVo.setId(order.getId());
            orderVo.setTitle(movieVo.getTitle());
            orderVo.setSeats(order.getSeats());
            orderVo.setTotalPrice(order.getTotalPrice());

            return orderVo;
        }).collect(Collectors.toList());
        //返回结果
        return orderVos;
    }
}
